﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DigitsRecognizer.NeuralNetwork;

namespace NeuralNetwork
{
    public class ReaderDoubleParams : Reader<double>
    {
        public override bool ReadParams(ref List<double[]> list, string login)
        {
            try
            {
                FileStream fs = new FileStream(string.Format("Params_{0}.txt", login), FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                final = sr.ReadToEnd();
                sr.Close();
                ConvertParams(ref list);
                return true;
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        protected override void ConvertParams(ref List<double[]> list)
        {
            list = new List<double[]>();
            int indexStart, indexEnd;
            string temp;
            string[] tableString;
            double[] tableDouble;
            double e;

            final = final.Replace("\n", " ");
            final = final.Replace("\t", " ");
            final = final.Replace("\r", " ");

            while (final.Length > 0)
            {
                indexStart = final.IndexOf("{E=") + 3;
                indexEnd = final.IndexOf("}");
                if (indexEnd == -1)
                {
                    break;
                }
                temp = final.Substring(indexStart, indexEnd - indexStart);
                e = Convert.ToDouble(temp);
                list.Add(new double[] { e });
                final = final.Substring(indexEnd + 1);
                indexStart = final.IndexOf("{") + 1;
                indexEnd = final.IndexOf("}");
                temp = final.Substring(indexStart, indexEnd - indexStart);
                final = final.Substring(indexEnd + 1);
                tableString = temp.Split();
                tableString = tableString.Where(s => s != string.Empty).ToArray<string>();
                tableDouble = new double[tableString.Length];
                for (int i = 0; i < tableString.Length; i++)
                {
                    tableDouble[i] = Convert.ToDouble(tableString[i]);
                }
                list.Add(tableDouble);
            }
        }

        public override bool ReadNeuralNetworkParams(ref List<double> e, ref List<int> NumberOfInputs, ref List<List<List<Neuron>>> HiddenLayer, ref List<List<Neuron>> OutputLayer, string login, WriteReadDestination destination)
        {
            return base.ReadNeuralNetworkParams(ref e, ref NumberOfInputs, ref HiddenLayer, ref OutputLayer, login, destination);
        }

        protected override void ConvertNeuralNetworkParams(ref List<double> e, ref List<int> NumberOfInputs, ref List<List<List<Neuron>>> HiddenLayer, ref List<List<Neuron>> OutputLayer)
        {
            base.ConvertNeuralNetworkParams(ref e, ref NumberOfInputs, ref HiddenLayer, ref OutputLayer);
        }
    }
}
